package com.datastructure.test.baseconversion;

public class BaseConversion {

    public static void main(String[] args) {
        BaseConversion baseConversion = new BaseConversion();
        String solve = baseConversion.solve(10, 16);
        System.out.println(solve);
    }

    public String solve (int M, int N) {
        // write code here
        boolean flag=false;
        if(M<0){
            flag=true;
            M=Math.abs(M);
        }
        StringBuilder stringBuilder = new StringBuilder();
        int i;
        if(M<N){
                switch (M){
                    case 10 :
                        stringBuilder.append("A");
                        break;
                    case 11 :
                        stringBuilder.append("B");
                        break;
                    case 12 :
                        stringBuilder.append("C");
                        break;
                    case 13 :
                        stringBuilder.append("D");
                        break;
                    case 14 :
                        stringBuilder.append("E");
                        break;
                    case 15 :
                        stringBuilder.append("F");
                        break;
                    default:
                        stringBuilder.append(M);
                        break;
                }
                return stringBuilder.toString();
        }
        while (M/N!=0||M%N!=0){
            i = M % N;
            if(i==10){
                stringBuilder.append("A");
                M= M/N;
                continue;
            }
            if(i==11){
                stringBuilder.append("B");
                M= M/N;
                continue;
            }
            if(i==12){
                stringBuilder.append("C");
                M= M/N;
                continue;
            }
            if(i==13){
                stringBuilder.append("D");
                M= M/N;
                continue;
            }
            if(i==14){
                stringBuilder.append("E");
                M= M/N;
                continue;
            }
            if(i==15){
                stringBuilder.append("F");
                M= M/N;
                continue;
            }
            stringBuilder.append(i);
            M= M/N;
        }
        if(flag){
            return "-"+stringBuilder.reverse().toString();
        }
        return stringBuilder.reverse().toString();
    }

    public String solve1 (int M, int N) {
        // write code here
        int m = Math.abs(M);
        String s = "0123456789ABCDEF";
        StringBuilder sb = new StringBuilder();
        while (m > 0) {
            sb.append(s.charAt(m % N));
            m /= N;
        }
        if (M < 0) sb.append("-");
        return sb.reverse().toString();
    }
}
